using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using EngineGame;
namespace WarsOnRiceFields
{
    /// <summary>
    /// Lop nay xu ly viec chon doi tuong, gioi thieu man choi moi
    /// </summary>
    class Introduction: GameScreen
    {
        //Input members
        TouchCollection touchState;
        private Thread thread;
        // Rendering Members
        //
        Texture2D showEnemies;
        Texture2D chooseFriends;
       // Texture2D goPress;

        SpriteFont font;
        public List<Sprite> listEnemies = new List<Sprite>();
        public List<MenuEntry> MenuEntries = new List<MenuEntry>();
        public List<Items> listItems = new List<Items>();
        public List<Items> listItemsChoosed = new List<Items>();
        bool isShowEnemies;
        public Introduction()
        {
            isShowEnemies = true;
            MenuEntry menuEntry = new MenuEntry("Menu", new Vector2(5, 440));
            MenuEntry startMenuEntry = new MenuEntry("Play", new Vector2(710, 440));
            startMenuEntry.Selected += StartMenuEntrySelected;
            menuEntry.Selected += menuEntrySelected;
            MenuEntries.Add(startMenuEntry);
            MenuEntries.Add(menuEntry);
            CreateItems();
            thread = new Thread(new ThreadStart(Count));
            thread.Start();
            
        }
        void StartMenuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new GamePlayScreen(listItemsChoosed));
            ScreenManager.RemoveScreen(this);
        }
        void menuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new Menu());
            this.ExitScreen();
        }
        private void Count()
        {
            Thread.Sleep(2000);
            isShowEnemies=false;
        }
        public override void LoadContent()
        {
            LoadEnemies();
            showEnemies = ScreenManager.Game.Content.Load<Texture2D>("Images/showenemies");
            chooseFriends = ScreenManager.Game.Content.Load<Texture2D>("Images/choosefriends");
            SpriteFont font = ScreenManager.Game.Content.Load<SpriteFont>("Arial");
            foreach (var item in listItems)
            {
                item.LoadContent(ScreenManager.Game.Content, font);
            }
           // font = ScreenManager.Game.Content.Load<SpriteFont>("Arial");
        }
        public override void UnloadContent()
        {

            base.UnloadContent();
        }
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);


        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
            if (isShowEnemies)
            {
                ScreenManager.SpriteBatch.Draw(showEnemies, new Vector2(0, 0), Color.White);
                for(int i=0;i<listEnemies.Count;i++)
                { 
                    listEnemies[i].Draw(ScreenManager.SpriteBatch,new Vector2(500,i*100+100),new Vector2(0,0),1.5f);
                    listEnemies[i].Next(gameTime);
                }
            }
            else
            {
                ScreenManager.SpriteBatch.Draw(chooseFriends, new Vector2(0, 0), Color.White);
                foreach (var item in listItems)
                {
                    item.Draw(ScreenManager.SpriteBatch);
                }
                /* foreach (var item1 in listItemsChoosed)
                 {
                     item1.Draw(ScreenManager.SpriteBatch);
                 }*/
                if (MenuEntries.Count > 0)
                {
                    SpriteFont font = ScreenManager.Font;
                    // Draw each menu entry in turn.
                    foreach (MenuEntry menuEntry in MenuEntries)
                    {
                        menuEntry.Draw(ScreenManager.SpriteBatch, font);
                    }
                }
            }
            ScreenManager.SpriteBatch.End();
            base.Draw(gameTime);
        }

        public override void HandleInput(InputState input)
        {
            if (input == null) throw new ArgumentNullException("input");
            if (input.PauseGame)
            {
                ScreenManager.AddScreen(new Menu());
                this.ExitScreen();
                return;
            }
            TouchCollection touchState = TouchPanel.GetState();
            if (isShowEnemies) return;
            if (touchState.Count > 0)
            {
                foreach (MenuEntry menuEntry in MenuEntries)
                {
                    menuEntry.OnSelectEntry(touchState[0]);
                }
                foreach (var item in listItems)
                {
                    if (item.OnSelected(touchState[0]))
                    {
                        if (item.isInList)
                        {
                            listItemsChoosed.Remove(item);
                            item.resetLocation();
                        }
                        else
                        {
                            item.setLocation(250 + 85 * (listItemsChoosed.Count - 1), 380);
                            item.isInList = true;
                            listItemsChoosed.Add(item);
                        }
                    }
                }
            }
            
        }
        private void CreateItems()
        {
            Items item;
            int tam = 550;
            item = new Items("Ant", "It can kill Insects", "Items/conkien", tam, 0);
            listItems.Add(item);
            item = new Items("Bee", "It can kill Plant Hoppers", "Items/conong", tam, 1);
            listItems.Add(item);
            item = new Items("Cat", "It can kill mices", "Items/conmeo", tam, 2);
            listItems.Add(item);
            item = new Items("Frog", "It can kill insects and Grass Hoppers", "Items/conech", tam, 3);
            listItems.Add(item);
            item = new Items("Mouse trap", "It can kill mices", "Items/baychuot", tam, 4);
            listItems.Add(item);
            item = new Items("Flowers", "A place for bees", "Items/caybong", tam, 5);
            listItems.Add(item);
            ///////////////////////
            tam = 60;
            item = new Items("Rice", "It can kill Insects", "Items/luagiong", tam, 0);
            listItems.Add(item);
            item = new Items("Manure", "It can kill Plant Hoppers", "Items/phanbon", tam, 1);
            listItems.Add(item);
            item = new Items("Cultivator", "It can kill mices", "Items/maycay", tam, 2);
            listItems.Add(item);
            item = new Items("Plane", "It can kill insects and Grass Hoppers", "Items/maybay", tam, 3);
            listItems.Add(item);
            item = new Items("Mouse trap", "It can kill mices", "Items/baychuot", tam, 4);
            listItems.Add(item);
            item = new Items("Flowers", "A place for bees", "Items/caybong", tam, 5);
            listItems.Add(item);
        }
        private void LoadEnemies() 
        { 
            String[] path={"sChuot","sOc","sSau"};
            Sprite temp;
            foreach(String s in path)
            {
                temp = new Sprite(ScreenManager.Game.Content, "Enemies/"+s, 4);
                listEnemies.Add(temp);
            }
        }
    }
}
